package com.maykk.Demo02;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName: CRUD
 * C create 新增数据
 * R read 查找数据
 * U update 更新数据
 * D delete 删除数据
 * @Description: TODO JDBC CRUD练习
 * @Author: MaykkStar@163.com
 */
public class CRUD {
    public static void main(String[] args) throws SQLException {
        String sql = "delete from stu where tel='12341245623'";
        // read
        // read(sql);
        // cud
        cud(sql);
    }

    private static void cud(String sql) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        Statement st = conn.createStatement();
        int i = st.executeUpdate(sql);
        System.out.println(i + "行受相应！~");
        JdbcUtils.free(null,st,conn);
    }

    private static void read(String sql) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        showRes(rs);
    }

    private static void showRes(ResultSet rs) throws SQLException {
        while (rs.next()) {
            System.out.printf(
                    "├————————————————————————————————————————————┥\n" +
                    "|%d\t|%3s\t|%s\t|%d\t|%4.1f\t|%s|\n" +
                    "├————————————————————————————————————————————┥\n",
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getObject("sex"),
                    rs.getInt("age"),
                    rs.getFloat("score"),
                    rs.getString("tel")
            );
        }
    }
}
